﻿using Dm.filter;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Diagnostics;
using System.Threading;
using WLYT.Application.Interfaces;
using WLYT.Common;
using WLYT.Domain.Entries;
using WLYT.Domain.Model;
using WLYT.WMS.Component;

namespace WLYT.WMS.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class WMSController(IBaseService<LogRecord> logService,
                               IOrderService orderService,
                               ILogger<MessageInterceptorMiddleware> logger) : ControllerBase
    {
        [HttpPost]
        public async Task<ApiResult> Post([FromBody]ApiParam apiParam)
        {
            ApiResult apiResult;
            long startTime = DateTime.Now.Ticks;
            LogRecord log = new LogRecord
            {
                Method = "POST",
                RequestUri = apiParam.ApiName,
                CreateTime = DateTime.Now,
                RequestContent = JsonConvert.SerializeObject(apiParam),
            };
            bool isLog = true;
            Stopwatch watch = new();
            try
            {
                switch (apiParam.ApiName)
                {
                    case "Wms.InStockFinish":
                        /*
                        {
                          "AppId": "",
                          "ApiName": "Wms.InStockFinish",
                          "ParamData": {
                                "WarehouseNo": "WAREHOUSE-1",
                                "LocationNo": "A1-1-1",
                                "ReelId": "138914S301125000660"
                          }
                        }
                         */
                        SMTModel model = apiParam.ParamData.ConvertObject<SMTModel>();
                        apiResult = await orderService.InStock(model);
                        break;
                    case "Wms.GetOutMateriel":
                        /*
                        {
                          "AppId": "",
                          "ApiName": "Wms.GetOutMateriel",
                          "ParamData": {
                                "ReelId": "138914S301125000660"
                          }
                        }
                         */
                        SMTModel outModel = apiParam.ParamData.ConvertObject<SMTModel>();
                        apiResult = await orderService.OutStock(outModel.ReelId);
                        break;
                    case "Wms.OutStockNG":
                        /*
                        {
                          "AppId": "",
                          "ApiName": "Wms.OutStockNG",
                          "ParamData": {
                                "ReelId": "138914S301125000660"
                          }
                        }
                         */
                        SMTModel ngModel = apiParam.ParamData.ConvertObject<SMTModel>();
                        apiResult = await orderService.OutStockNG(ngModel.ReelId);
                        break;
                    case "Wms.PickCompleted":
                        /*
                        {
                          "AppId": "",
                          "ApiName": "Wms.PickCompleted",
                          "ParamData": {
                                "ReelId": "138914S301125000660",
                                "BoxCode":"箱号",
                          }
                        }
                         */
                        SMTModel boxModel = apiParam.ParamData.ConvertObject<SMTModel>();
                        apiResult = await orderService.PickCompleted(boxModel.ReelId, boxModel.BoxCode);
                        break;
                    case "Wms.BoxArriveEnd":
                        /*
                        {
                          "AppId": "",
                          "ApiName": "Wms.BoxArriveEnd",
                          "ParamData": {
                                "BoxCode":"箱号",
                          }
                        }
                         */
                        SMTModel endModel = apiParam.ParamData.ConvertObject<SMTModel>();
                        apiResult = await orderService.BoxArriveEnd(endModel.BoxCode);
                        break;
                    default:
                        throw new Exception("找不到该接口");
                }
            }
            catch (Exception ex)
            {
                logger.LogError($"调用WMS【{apiParam.ApiName}】报错，错误信息：{ex.Message}");
                apiResult = new ApiResult { Code = 500, Message = "WMS：" + ex.Message};
            }

            if (isLog)
            {
                watch.Stop();
                log.ResponseContent = JsonConvert.SerializeObject(apiResult);
                log.Timer = (int)watch.ElapsedMilliseconds;
                log.IsSuccess = apiResult.Code == 0;
                await logService.SplitTableInsertAsync(log);
            };
            return apiResult;
        }
    }
}
